package com.ruoyi.business.domain.enums;

import java.util.ArrayList;
import java.util.List;

/**
 * 支付方式枚举
 */
public enum PaymentMethod {

    /**
     * 微信支付
     */
    WECHAT_PAY(1, "微信支付", "微信扫码支付、小程序支付等"),

    /**
     * 支付宝
     */
    ALIPAY(2, "支付宝", "支付宝扫码支付、App支付等"),

    /**
     * 纯积分支付
     */
    POINTS_ONLY(3, "纯积分", "完全使用积分支付"),

    /**
     * KOC产品兑换
     */
    KOC_EXCHANGE(4, "KOC产品兑换", "KOC专属产品兑换"),

    /**
     * 积分+金额混合支付
     */
    POINTS_AND_MONEY(5, "积分+金额", "积分和金额组合支付");

    private final Integer code;
    private final String name;
    private final String description;

    PaymentMethod(Integer code, String name, String description) {
        this.code = code;
        this.name = name;
        this.description = description;
    }

    /**
     * 根据code获取枚举
     */
    public static PaymentMethod getByCode(Integer code) {
        if (code == null) {
            return null;
        }
        for (PaymentMethod method : values()) {
            if (method.getCode().equals(code)) {
                return method;
            }
        }
        throw new IllegalArgumentException("未知的支付方式码: " + code);
    }

    /**
     * 根据name获取枚举
     */
    public static PaymentMethod getByName(String name) {
        if (name == null || name.trim().isEmpty()) {
            return null;
        }
        for (PaymentMethod method : values()) {
            if (method.getName().equals(name.trim())) {
                return method;
            }
        }
        throw new IllegalArgumentException("未知的支付方式名称: " + name);
    }

    /**
     * 检查是否为现金支付方式
     */
    public boolean isCashPayment() {
        return this == WECHAT_PAY || this == ALIPAY;
    }

    /**
     * 检查是否为积分支付方式
     */
    public boolean isPointsPayment() {
        return this == POINTS_ONLY || this == POINTS_AND_MONEY;
    }

    /**
     * 检查是否为纯积分支付
     */
    public boolean isPurePoints() {
        return this == POINTS_ONLY;
    }

    /**
     * 检查是否为混合支付
     */
    public boolean isMixedPayment() {
        return this == POINTS_AND_MONEY;
    }

    /**
     * 检查是否为KOC专属支付
     */
    public boolean isKocPayment() {
        return this == KOC_EXCHANGE;
    }

    /**
     * 检查是否需要调用第三方支付
     */
    public boolean requiresThirdPartyPayment() {
        return this == WECHAT_PAY || this == ALIPAY;
    }

    /**
     * 检查是否需要审批流程
     */
    public boolean requiresApproval() {
        return this == POINTS_ONLY || this == POINTS_AND_MONEY;
    }

    /**
     * 获取支持的订单来源
     */
    public List<OrderSource> getSupportedSources() {
        List<OrderSource> sources = new ArrayList<>();

        switch (this) {
            case WECHAT_PAY:
            case ALIPAY:
                sources.add(OrderSource.NORMAL_MALL);
                sources.add(OrderSource.POINTS_MALL);
                break;
            case POINTS_ONLY:
            case POINTS_AND_MONEY:
                sources.add(OrderSource.POINTS_MALL);
                break;
            case KOC_EXCHANGE:
                sources.add(OrderSource.POINTS_MALL);
                break;
        }

        return sources;
    }

    /**
     * 获取支持的订单类型
     */
    public List<OrderType> getSupportedOrderTypes() {
        List<OrderType> types = new ArrayList<>();

        switch (this) {
            case WECHAT_PAY:
            case ALIPAY:
                types.add(OrderType.NORMAL);
                types.add(OrderType.POINTS_AND_MONEY);
                break;
            case POINTS_ONLY:
                types.add(OrderType.POINTS_ONLY);
                break;
            case POINTS_AND_MONEY:
                types.add(OrderType.POINTS_AND_MONEY);
                break;
            case KOC_EXCHANGE:
                types.add(OrderType.KOC_EXCHANGE);
                break;
        }

        return types;
    }

    /**
     * 验证支付方式和订单来源的兼容性
     */
    public boolean supportsOrderSource(OrderSource source) {
        return getSupportedSources().contains(source);
    }

    /**
     * 验证支付方式和订单类型的兼容性
     */
    public boolean supportsOrderType(OrderType orderType) {
        return getSupportedOrderTypes().contains(orderType);
    }

    /**
     * 获取支付方式图标（前端使用）
     */
    public String getIcon() {
        switch (this) {
            case WECHAT_PAY:
                return "wechat";
            case ALIPAY:
                return "alipay";
            case POINTS_ONLY:
                return "gift";
            case KOC_EXCHANGE:
                return "star";
            case POINTS_AND_MONEY:
                return "transaction";
            default:
                return "pay-circle";
        }
    }

    /**
     * 获取支付方式颜色（前端使用）
     */
    public String getColor() {
        switch (this) {
            case WECHAT_PAY:
                return "#09BB07";
            case ALIPAY:
                return "#1677FF";
            case POINTS_ONLY:
                return "#52C41A";
            case KOC_EXCHANGE:
                return "#722ED1";
            case POINTS_AND_MONEY:
                return "#FA8C16";
            default:
                return "#D9D9D9";
        }
    }

    // Getters
    public Integer getCode() {
        return code;
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    @Override
    public String toString() {
        return String.format("PaymentMethod{code=%d, name='%s', description='%s'}",
            code, name, description);
    }
}
